/*
 * Copyright Eduardo Aponte 2013
 * aponteeduardo@gmail.com
 */

/* Utilities */


#include <eyelink.h>
#include "mex.h"
#include "mex_utils.h"

int isscalar (const mxArray *ia){
    if (mxIsNumeric(ia) || mxIsLogical(ia)){
        const int *sia;
        mwSize dia = mxGetNumberOfDimensions(ia);
        sia = mxGetDimensions(ia);
        if ( dia == 2 ){
            if (sia[0] == 1 && sia[1] == 1){
                return 1;
                } else {
                    return 0;
                    }
            } else {
                return 0;
                }
} else {
        return 0;
        }
}


int returnscalar (const mxArray *ia, int *oi ){
    mxClassID id =  mxGetClassID( ia );
    
    if (mxIsComplex(ia)){
        return 0;
    }
    
   if ( id == mxLOGICAL_CLASS ){
        mxLogical  *td;
        td = (mxLogical  *)mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxINT8_CLASS ){
        signed char *td = (signed char * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxUINT8_CLASS ){
        unsigned char *td = (unsigned char * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxUINT16_CLASS ){
        unsigned short *td = (unsigned short * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxINT16_CLASS ){
        short *td = (short * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxUINT32_CLASS ){
        unsigned int *td = (unsigned int * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxINT32_CLASS ){
        int *td = (int * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxINT64_CLASS ){
        long long *td = (long long * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxUINT64_CLASS ){
        unsigned long long * td = (unsigned long long * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxSINGLE_CLASS ){
        float * td = (float * ) mxGetData(ia);
        *oi = (int ) *td;
        return 1;
    } else if ( id == mxDOUBLE_CLASS ){
            *oi = (int ) *mxGetPr(ia);
            return 1;
    }
    
    return 0;
}

